<?php

namespace common\service;

use Yii;
use yii\helpers\Url;

class GlobalUrlService extends BaseService
{
    /**
     * 生成静态的资源链接.
     * @param $path
     * @param array $params
     * @return string
     */
    public static function buildStaticUrl($path,$params = [])
    {
        $domain = Yii::$app->params['domains']['static'];
        if( stripos($domain,"http") === false ){
            $domain = "http:".$domain;
        }
        $path = Url::toRoute(array_merge([$path],$params));
        return $domain.$path;
    }

    /**
     * 生成总后台链接
     * @param $path
     * @param array $params
     * @return string
     */
    public static function buildAdminUrl($path,$params = [])
    {
        $domain = Yii::$app->params['domains']['admin'];

        if(stripos($domain,'http') === false)
        {
            $domain = "http:".$domain;
        }

        return $domain . Url::toRoute(array_merge([$path],$params));
    }

    /**
     * 生成商家后台链接
     * @param $path
     * @param array $params
     * @return string
     */
    public static function buildShopUrl($path,$params = [])
    {
        $domain = Yii::$app->params['domains']['shop'];
        if( stripos($domain,"http") === false ){
            $domain = "http:".$domain;
        }

        return $domain . Url::toRoute(array_merge([$path],$params));
    }

    /**
     * 生成商家静态的资源链接.
     * @param $path
     * @param array $params
     * @return string
     */
    public static function buildShopStaticUrl($path,$params = [])
    {
        $domain = Yii::$app->params['domains']['shop_static'];
        if( stripos($domain,"http") === false ){
            $domain = "http:".$domain;
        }
        $path = Url::toRoute(array_merge([$path],$params));
        return $domain.$path;
    }

    /**
     * 生成cdn链接
     * @param $path
     * @return string
     */
    public static function buildCdnUrl($path)
    {
        $domain = Yii::$app->params['domains']['cdn'];

        if (stripos($domain, 'http') === false) {
            $domain = "http:" . $domain;
        }
        return $domain . '/' . $path;
    }
    /**
     * 生成api链接
     * @param string$path
     * @param array $params
     * @return string
     */
    public static function buildApiUrl($path, $params = [])
    {

        $domain = Yii::$app->params['domains']['api'];
        if( stripos($domain,"http") === false ){
            $domain = "http:".$domain;
        }
        $path = Url::toRoute(array_merge([$path],$params));
        return $domain.$path;
    }

}